<?php
/*
  $Program: ECJC Online Shop
  $Id: html_output.php
  $Based on: osCommerce, Open Source E-Commerce Solutions
  Released under the GNU General Public License
*/
// Creates a pull-down list of countries
 function tep_get_country_list($name, $selected = '', $parameters = '') {
   $countries = tep_get_countries();

   return tep_draw_pull_down_menu($name, $countries, $selected, 

$parameters);
 }
//MVS End
  // --------------------------------------------------------------------------------------------------------------
  // create a self reference to the a osCommerce module
  function modSefRef ( $osModName = null ) {
    global $osMod;
    if ( !$osModName ) { $osModName = $osMod; };
    return $osModName . '.php';
  }; // modSefRef

  // --------------------------------------------------------------------------------------------------------------
  // remove parameters from a request url
  function remove_param( $param_str, $remove ) {
    $param_str = preg_replace( '/\&|\?/', "{SEP}", $param_str );
    $param_arr = split( '{SEP}', $param_str );
    $final_val = null;
    foreach ( $param_arr as $param ) {
      $pass = 1;
      foreach ( $remove as $key ) {
        if ( preg_match( "/^$key/", $param ) ) { $pass = 0; break; }
      }
      if ( $pass ) { $final_val .= $param . '&'; };
    }
    $final_val = preg_replace( '/\&+$/', '', $final_val );
    return $final_val;
  }; // remove_params

  // --------------------------------------------------------------------------------------------------------------
  // The HTML href link wrapper function
  function tep_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true) {
    global $request_type, $session_started, $SID;

    if (!tep_not_null($page)) {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine the page link!<br><br>');
    }

    if ($connection == 'NONSSL') {
      $link = HTTP_SERVER . DIR_WS_ADMIN;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL == true) {
        $link = HTTPS_SERVER . DIR_WS_ADMIN;
      } else {
        $link = HTTP_SERVER . DIR_WS_ADMIN;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL</b><br><br>');
    }

    // -- webMS Modification --
    $page_id = preg_replace( '/\.php/', '', $page);
    if ( preg_match( '/popup/', $page_id ) || preg_match( '/popup/', $parameters ) ) {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index3.', $page);
    } else {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index3.', $page);
    }
    $link .= $page_ud;
    $parameters = remove_param( $parameters, array('option','osMod','osCsid') );

    // Debug Line
    // echo "PAGE[ $page ] PAGE_ID [ $page_id ] PAGE_UD [ $page_id ] LINK [ $link ] PARAMETERS [ $parameters ]";

    if ( $parameters != 'NONE' ) {
      if (preg_match('/\?/',$page)){
        if ($parameters == '') {
          $link .= '&option=com_oscommerce&osMod=' . $page_id;
        } else {
          $link .= '&option=com_oscommerce&osMod=' . $page_id . '&' . $parameters;
        }
      } else {
        if ($parameters == '') {
          $link .= '?option=com_oscommerce&osMod=' . $page_id;
        } else {
          $link .= '?option=com_oscommerce&osMod=' . $page_id . '&' . $parameters;
        }
      }
    }
    $separator = '&';

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    // Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
    if ( ($add_session_id == true) && ($session_started == true) && (SESSION_FORCE_COOKIE_USE == 'False') ) {
      if (tep_not_null($SID)) {
        $_sid = $SID;
      } elseif ( ( ($request_type == 'NONSSL') && ($connection == 'SSL') && (ENABLE_SSL == true) ) || ( ($request_type == 'SSL') && ($connection == 'NONSSL') ) ) {
        if (HTTP_COOKIE_DOMAIN != HTTPS_COOKIE_DOMAIN) {
          $_sid = tep_session_name() . '=' . tep_session_id();
        }
      }
    }

    if (isset($_sid)) {
      $link .= $separator . $_sid;
    }

    // Debug Line
    // echo " RETURN [ $link ][ $_sid ] <br/>";

    return $link;
  }; // END tep_href_link

  // --------------------------------------------------------------------------------------------------------------
  // The HTML href link wrapper function for catalog
  function tep_catalog_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true) {
    global $request_type, $session_started, $SID;

    if (!tep_not_null($page)) {
      $page = 'index.php';
    }

    if ($connection == 'NONSSL') {
      $link = HTTP_CATALOG_SERVER . DIR_WS_CATALOG;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL_CATALOG == 'true') {
        $link = HTTPS_CATALOG_SERVER . DIR_WS_CATALOG;
      } else {
        $link = HTTP_CATALOG_SERVER . DIR_WS_CATALOG;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL<br><br>Function used:<br><br>tep_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }

    // -- webMS Modification --
    $page_id = preg_replace( '/\.php/', '', $page);
    if ( preg_match( '/popup/', $page_id ) ) {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index2.', $page);
    } else {
      $page_ud = preg_replace( '/^[\w_]+\./', 'index.', $page);
    }
    $link .= $page_ud;
    $parameters = remove_param( $parameters, array('option','osMod','osCsid') );

    // Debug Line
    // echo "PAGE[ $page ] PAGE_ID [ $page_id ] PAGE_UD [ $page_id ] LINK [ $link ] PARAMETERS [ $parameters ]";

    if ( $parameters != 'NONE' ) {
      if (preg_match('/\?/',$page)){
        if ($parameters == '') {
          $link .= '&option=com_oscommerce&osMod=' . $page_id;
        } else {
          $link .= '&' . $parameters . '&option=com_oscommerce&osMod=' . $page_id;
        }
      } else {
        if ($parameters == '') {
          $link .= '?option=com_oscommerce&osMod=' . $page_id;
        } else {
          $link .= '?' . $parameters . '&option=com_oscommerce&osMod=' . $page_id;
        }
      }
    }
    $separator = '&';

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    // Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
    if ( ($add_session_id == true) && ($session_started == true) && (SESSION_FORCE_COOKIE_USE == 'False') ) {
      if (tep_not_null($SID)) {
        $_sid = $SID;
      } elseif ( ( ($request_type == 'NONSSL') && ($connection == 'SSL') && (ENABLE_SSL == true) ) || ( ($request_type == 'SSL') && ($connection == 'NONSSL') ) ) {
        if (HTTP_COOKIE_DOMAIN != HTTPS_COOKIE_DOMAIN) {
          $_sid = tep_session_name() . '=' . tep_session_id();
        }
      }
    }



    if (isset($_sid)) {
      $link .= $separator . $_sid;
    }

    // Debug Line
    // echo " RETURN [ $link ][ $_sid ] <br/>";

    return $link;
  }; // END tep_catalog_href_link

  // --------------------------------------------------------------------------------------------------------------
  // The HTML image wrapper function
  function tep_image($src, $alt = '', $width = '', $height = '', $params = '') {
    $image = '<img src="' . $src . '" border="0" alt="' . $alt . '"';
    if ($alt) {
      $image .= ' title=" ' . $alt . ' "';
    }
    if ($width) {
      $image .= ' width="' . $width . '"';
    }
    if ($height) {
      $image .= ' height="' . $height . '"';
    }
    if ($params) {
      $image .= ' ' . $params;
    }
    $image .= '>';

    return $image;
  }

  // --------------------------------------------------------------------------------------------------------------
  // The HTML form submit button wrapper function
  // Outputs a button in the selected language
  function tep_image_submit($image, $alt = '', $parameters = '') {
    global $language;

    $image_submit = '<input type="image" src="' . tep_output_string(DIR_FS_LANGUAGES . $language . '/images/buttons/' . $image) . '" border="0" alt="' . tep_output_string($alt) . '"';

    if (tep_not_null($alt)) $image_submit .= ' title=" ' . tep_output_string($alt) . ' "';

    if (tep_not_null($parameters)) $image_submit .= ' ' . $parameters;

    $image_submit .= '>';

    return $image_submit;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Draw a 1 pixel black line
  function tep_black_line() {
    return tep_image(DIR_WS_IMAGES . 'pixel_black.gif', '', '100%', '1');
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a separator either through whitespace, or with an image
  function tep_draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
    return tep_image(DIR_WS_IMAGES . $image, '', $width, $height);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a function button in the selected language
  function tep_image_button($image, $alt = '', $params = '') {
    global $language;

    return tep_image(DIR_FS_LANGUAGES . $language . '/images/buttons/' . $image, $alt, '', '', $params);
  }

  // --------------------------------------------------------------------------------------------------------------
  // javascript to dynamically update the states/provinces list when the country is changed
  // TABLES: zones
  function tep_js_zone_list($country, $form, $field) {
    $countries_query = tep_db_query("select distinct zone_country_id from " . TABLE_ZONES . " order by zone_country_id");
    $num_country = 1;
    $output_string = '';
    while ($countries = tep_db_fetch_array($countries_query)) {
      if ($num_country == 1) {
        $output_string .= '  if (' . $country . ' == "' . $countries['zone_country_id'] . '") {' . "\n";
      } else {
        $output_string .= '  } else if (' . $country . ' == "' . $countries['zone_country_id'] . '") {' . "\n";
      }

      $states_query = tep_db_query("select zone_name, zone_id from " . TABLE_ZONES . " where zone_country_id = '" . $countries['zone_country_id'] . "' order by zone_name");

      $num_state = 1;
      while ($states = tep_db_fetch_array($states_query)) {
        if ($num_state == '1') $output_string .= '    ' . $form . '.' . $field . '.options[0] = new Option("' . PLEASE_SELECT . '", "");' . "\n";
        $output_string .= '    ' . $form . '.' . $field . '.options[' . $num_state . '] = new Option("' . $states['zone_name'] . '", "' . $states['zone_id'] . '");' . "\n";
        $num_state++;
      }
      $num_country++;
    }
    $output_string .= '  } else {' . "\n" .
                      '    ' . $form . '.' . $field . '.options[0] = new Option("' . TYPE_BELOW . '", "");' . "\n" .
                      '  }' . "\n";

    return $output_string;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form
  function tep_draw_form($name, $action, $method = 'post', $parameters = '') {
    $action_url = preg_replace( '/\?.*/','',$action);
    $action_par = preg_replace( '/.*\?/','',$action);

    $form = '<form name="' . tep_output_string($name) . '" action="' . tep_output_string($action_url) . '" method="' . tep_output_string($method) . '"';

    if (tep_not_null($parameters)) $form .= ' ' . $parameters;

    $form .= '>';

    if ($action_par != '') {
      $action_params = array();
      $action_params = split('&',$action_par);
      foreach ($action_params AS $this_par) {
        list($key, $value) = split('=',$this_par);
        $form .= tep_draw_hidden_field( $key, $value );
      };
    }

    return $form;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form input field
  function tep_draw_input_field($name, $value = '', $parameters = '', $required = false, $type = 'text', $reinsert_value = true) {
    global $_GET, $_POST;
    $field = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    //if (isset($GLOBALS[$name]) && ($reinsert_value == true) && is_string($GLOBALS[$name])) {
    //  $field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
	if ( ($reinsert_value == true) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {  
	if (isset($_GET[$name]) && is_string($_GET[$name])) {
	  $value = stripslashes($_GET[$name]);
	} elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
	$value = stripslashes($_POST[$name]);
	}
   }
    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    if ($required == true) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form password field
  function tep_draw_password_field($name, $value = '', $required = false) {
    $field = tep_draw_input_field($name, $value, 'maxlength="40"', $required, 'password', false);

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form filefield
  function tep_draw_file_field($name, $required = false) {
    $field = tep_draw_input_field($name, '', '', $required, 'file');

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a selection field - alias function for tep_draw_checkbox_field() and tep_draw_radio_field()
  function tep_draw_selection_field($name, $type, $value = '', $checked = false, $compare = '') {
    global $_GET, $_POST;
    $selection = '<input type="' . tep_output_string($type) . '" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) $selection .= ' value="' . tep_output_string($value) . '"';


    if ( ($checked == true) || (isset($_GET[$name]) && is_string($_GET[$name]) && (($_GET[$name] == 'on') || (stripslashes($_GET[$name]) == $value))) || (isset($_POST[$name]) && is_string($_POST[$name]) && (($_POST[$name] == 'on') || (stripslashes($_POST[$name]) == $value))) || (tep_not_null($compare) && ($value == $compare)) ) {
	  $selection .= ' CHECKED';
    }

    $selection .= '>';

    return $selection;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form checkbox field
  function tep_draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
    return tep_draw_selection_field($name, 'checkbox', $value, $checked, $compare);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form radio field
  function tep_draw_radio_field($name, $value = '', $checked = false, $compare = '') {
    return tep_draw_selection_field($name, 'radio', $value, $checked, $compare);
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form textarea field
  function tep_draw_textarea_field($name, $wrap, $width, $height, $text = '', $parameters = '', $reinsert_value = true) {
    global $_GET, $_POST;
    $field = '<textarea name="' . tep_output_string($name) . '" wrap="' . tep_output_string($wrap) . '"';
    if ( $width ) $field .= ' cols="' . tep_output_string($width) . '"';
    if ( $height ) $field .= ' rows="' . tep_output_string($height) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

   //if ( (isset($GLOBALS[$name])) && ($reinsert_value == true) ) {
	if ( ($reinsert_value == true) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
		if (isset($_GET[$name]) && is_string($_GET[$name])) {
 	 //$field .= tep_output_string_protected(stripslashes($GLOBALS[$name]));
	  $field .= tep_output_string_protected(stripslashes($_GET[$name]));
	} elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
	  $field .= tep_output_string_protected(stripslashes($_POST[$name]));
   }
} elseif (tep_not_null($text)) {

  $field .= tep_output_string_protected($text);

}



    $field .= '</textarea>';

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form hidden field
  function tep_draw_hidden_field($name, $value = '', $parameters = '') {
  	global $_GET, $_POST;
    $field = '<input type="hidden" name="' . tep_output_string($name) . '"';

    if (tep_not_null($value)) {
      $field .= ' value="' . tep_output_string($value) . '"';
    //} elseif (isset($GLOBALS[$name]) && is_string($GLOBALS[$name])) {
	} elseif ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) {
	  if ( (isset($_GET[$name]) && is_string($_GET[$name])) ) {
	    $field .= ' value="' . tep_output_string(stripslashes($_GET[$name])) . '"';
	  } elseif ( (isset($_POST[$name]) && is_string($_POST[$name])) ) {
	    $field .= ' value="' . tep_output_string(stripslashes($_POST[$name])) . '"';
	  }
      //$field .= ' value="' . tep_output_string(stripslashes($GLOBALS[$name])) . '"';
    }

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    return $field;
  }

  // --------------------------------------------------------------------------------------------------------------
  // Output a form pull down menu
  function tep_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    global $_GET, $_POST;
    $field = '<select name="' . tep_output_string($name) . '"';

    if (tep_not_null($parameters)) $field .= ' ' . $parameters;

    $field .= '>';

    //if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	if (empty($default) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
		if (isset($_GET[$name]) && is_string($_GET[$name])) {
		   $default = stripslashes($_GET[$name]);
		} elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
		   $default = stripslashes($_POST[$name]);
		}
	  }

    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
      if ($default == $values[$i]['id']) {
        $field .= ' SELECTED';
      }

      $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
    }
    $field .= '</select>';

    if ($required == true) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }
  
   // Output a form multiple select menu
      function tep_draw_mselect_menu($name, $values, $selected_vals, $params = '', $required = false) {
        $field = '<select name="' . $name . '"';
        if ($params) $field .= ' ' . $params;
        $field .= ' multiple>';
        for ($i=0; $i<sizeof($values); $i++) {
                if ($values[$i]['id'])
                {
                $field .= '<option value="' . $values[$i]['id'] . '"';
                if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) ) {
                  $field .= ' SELECTED';
                }
                            else
                            {
                                       for ($j=0; $j<sizeof($selected_vals); $j++) {
                                                    if ($selected_vals[$j]['id'] == $values[$i]['id'])
                                                    {
                                                $field .= ' SELECTED';
                                                    }
                                        }
                            }
                }
          $field .= '>' . $values[$i]['text'] . '</option>';
        }
        $field .= '</select>';
           if ($required) $field .= TEXT_FIELD_REQUIRED;
             return $field;
    }
?>
